package speelotheek.views;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.sql.Date;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.BoxLayout;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;

import speelotheek.models.Kind;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;

public class Childeren extends JDialog 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 136233076116187356L;
	
	private JScrollPane scrollPane;
	private JList childList;
	private ArrayList<Kind> childArray;
	private Vector childeren;
	private Kind kind;
	private Kind child;
	private JTextField txtChildName;
	private JTextField txtChildBirth;
	private JRadioButton rdbtnJongen;
	private JRadioButton rdbtnMeisje;
	private JButton btnWijzigen;
	private JButton btnOpslaan;
	private ButtonGroup buttonGroup = new ButtonGroup();
	
	private ArrayList<String> customerErrors;
	
	private boolean _fieldStatus;

	/**
	 * Create the dialog.
	 */
	public Childeren(final String lidnr) 
	{
		setTitle("Kinderen");
		setBounds(100, 100, 540, 310);
		setModal(true);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Kinderen Lijst", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		getContentPane().add(panel);
		
		btnOpslaan = new JButton("Opslaan");
		btnOpslaan.setEnabled(false);
		btnOpslaan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg1) {
				String msgOne = "Weet u zeker dat u dit kind wilt wijzigen?";
				int confirmChange = JOptionPane.showConfirmDialog(null, msgOne, "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				switch(confirmChange) {
					case JOptionPane.YES_OPTION:	
						try {					
							_changeChild();
							String msgThree = "Kind succesvol gewijzigd";
							JOptionPane.showMessageDialog(null, msgThree, "Succes", JOptionPane.INFORMATION_MESSAGE);
						} 
						catch (Exception ex) {
							String msgTwo = "Database Error\n\n Neem contact op met de beheerder als die probleem zich blijft voordoen.";
							JOptionPane.showMessageDialog(null, msgTwo, "Error", JOptionPane.ERROR_MESSAGE);
							return;
						}	
						break;
					case JOptionPane.NO_OPTION:
						break;
				}							
				_changeFields(false);
				btnWijzigen.setText("Wijzigen");
				btnOpslaan.setEnabled(false);
			}
		});
		
		btnWijzigen = new JButton("Wijzigen");
		btnWijzigen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(_fieldStatus) {
					_restoreFieldValues();
					_changeFields(false);
					btnWijzigen.setText("Wijzigen");
					btnOpslaan.setEnabled(false);
				} 
				else {
					_changeFields(true);
					btnWijzigen.setText("Terugzetten");
					btnOpslaan.setEnabled(true);
				}
			}
		});
		
		kind = new Kind();
		childeren = new Vector();
		
		int lidId = Integer.parseInt(lidnr);
		try {
			childArray = kind.getChilderen(lidId);
			if(!childArray.isEmpty()) {
				for(int i = 0; i < childArray.size(); i++) {
					String childNaam = childArray.get(i).getNaam();
					String childId = Integer.toString(childArray.get(i).getId());	
					childeren.addElement(childId+" - "+childNaam);
				}
			}
			else {
				System.out.println("false");
				btnWijzigen.setEnabled(false);
			}
		} 
		catch (Exception ex) {
			btnWijzigen.setEnabled(false);
			System.out.println("Er gaat iets fout: "+ex.toString());
		}
		
		childList = new JList(childeren);
		childList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if(e.getSource() == childList && !e.getValueIsAdjusting()) {
					String stringValue = (String)childList.getSelectedValue();
					if(!stringValue.isEmpty()) {
						stringValue = stringValue.replaceAll("[^0-9]", "");
						int kindId = Integer.parseInt(stringValue);
						child = new Kind();
						try {
							child.getChild(kindId);
						} catch (Exception ex) {
							System.out.println("Er gaat iets fout: "+ex.toString());
						}
						txtChildName.setText(child.getNaam());
						if(child.getGeslacht() == "M") {
							rdbtnJongen.setSelected(true);
							rdbtnMeisje.setSelected(false);
						}
						else {
							rdbtnJongen.setSelected(false);
							rdbtnMeisje.setSelected(true);
						}
						txtChildBirth.setText(child.getGeboortedatum().toString());
					}
				}
			}
		});
		
		scrollPane = new JScrollPane(childList);
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 592, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(254, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Kind Info", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		getContentPane().add(panel_1);
		
		JLabel lblGeboortedatum = new JLabel("Geboortedatum:");
		
		JLabel lblGeslacht = new JLabel("Geslacht:");
		
		JLabel lblNaam = new JLabel("Naam:");
		
		txtChildName = new JTextField();
		txtChildName.setEnabled(false);
		txtChildName.setColumns(10);
		
		txtChildBirth = new JTextField();
		txtChildBirth.setEnabled(false);
		txtChildBirth.setColumns(10);
		
		rdbtnJongen = new JRadioButton("Jongen");
		rdbtnJongen.setEnabled(false);
		
		rdbtnMeisje = new JRadioButton("Meisje");
		rdbtnMeisje.setEnabled(false);
		
		JLabel lblJjjjmmdd = new JLabel("JJJJ-MM-DD");
		
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblNaam)
						.addComponent(lblGeboortedatum)
						.addComponent(lblGeslacht))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
								.addComponent(txtChildName, GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
								.addComponent(txtChildBirth))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lblJjjjmmdd)
							.addPreferredGap(ComponentPlacement.RELATED, 195, Short.MAX_VALUE)
							.addComponent(btnOpslaan))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(rdbtnJongen)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(rdbtnMeisje)
							.addPreferredGap(ComponentPlacement.RELATED, 285, Short.MAX_VALUE)
							.addComponent(btnWijzigen)))
					.addContainerGap())
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNaam)
						.addComponent(txtChildName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(11)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblGeslacht)
						.addComponent(rdbtnJongen)
						.addComponent(rdbtnMeisje)
						.addComponent(btnWijzigen))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblGeboortedatum)
						.addComponent(txtChildBirth, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblJjjjmmdd)
						.addComponent(btnOpslaan))
					.addContainerGap(31, Short.MAX_VALUE))
		);
		panel_1.setLayout(gl_panel_1);
	}
	
	private void _changeFields(boolean en)
	{
		txtChildName.setEnabled(en);
		txtChildBirth.setEnabled(en);
		rdbtnJongen.setEnabled(en);
		rdbtnMeisje.setEnabled(en);
		
		_fieldStatus = en;
	}
	
	private void _restoreFieldValues()
	{
		String childName = child.getNaam();
		String childGender = child.getGeslacht();
		Date childDate = (Date) child.getGeboortedatum();
		
		txtChildName.setText(childName);
		txtChildBirth.setText(childDate.toString());
		if(childGender == "M") {
			rdbtnJongen.setSelected(true);
			rdbtnMeisje.setSelected(false);
		}
		else {
			rdbtnJongen.setSelected(false);
			rdbtnMeisje.setSelected(true);
		}
	}
	
	private void _changeChild() throws Exception
	{		
		// Alle text in de textfields ophalen
		String naam = txtChildName.getText();
		String geboortedatum = txtChildBirth.getText();
		String geslacht = "";
		
		if(rdbtnJongen.isSelected()) {
			geslacht = "M";
		}
		if(rdbtnMeisje.isSelected()) {
			geslacht = "V";
		}	
		
		customerErrors = new ArrayList<String>();
		
		if(naam.isEmpty()) {
			customerErrors.add("U heeft geen naam ingevuld");
		}
		if(geslacht.isEmpty()) {
			customerErrors.add("U heeft geen geslacht aangeklikt");
		}
		if(geboortedatum.isEmpty()) {
			customerErrors.add("U heeft geen geboortedatum ingevuld");
		}
		
		if(customerErrors.isEmpty() != true) {
			throw new Exception("Errors on inputfields");
		}
		
		child.setNaam(naam);
		child.setGeboortedatum(Date.valueOf(geboortedatum));
		child.setGeslacht(geslacht);
		child.updateChild();
	}
}
